package cm.cty;

import com.cty.mapper.CustomerMapper;
import com.cty.mapper.OrderMapper;
import com.cty.pojo.Customer;
import com.cty.pojo.Order;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class test {

    public SqlSession sqlSession;

    @BeforeEach
    public void before() throws IOException {
        InputStream inputStream =
                Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession= sqlSessionFactory.openSession(true);
    }

    @AfterEach
    public void after() {
        sqlSession.close();
    }

    @Test
    public void test_01() {
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        Order order = mapper.queryById(1);
        System.out.println(order);
    }

    @Test
    public void test_02() {
        CustomerMapper mapper = sqlSession.getMapper(CustomerMapper.class);
        List<Customer> customerList = mapper.queryAll();
        System.out.println("customerList = " + customerList);
        for (Customer c: customerList) {
            List<Order> orderList = c.getOrderList();
            System.out.println("orderList = " + orderList);
        }
    }
}
